from amplitude.model import Model
import rights, users

class Role(Model):
    relation = 'roles'
    pk_fields = ['name']

    def rights(self, update=False):
        """many-to-many relationship through rights_roles"""
        if update==True or self._rights is None:
            self._rights = rights.Right(self.db).select(where="name in (select rightname from rights_roles where rolename='%(name)s')" % self)
        return self._rights

    def users(self, update=False):
        """has-many users"""
        if update==True or self._users is None:
            self._users = users.User(self.db).select(where="role='%(name)s'" % self)
        return self._users
